#include "cabandonbook.h"
#include "socket/protocol/ClientAbandonBook.h"
#include <QDataStream>
#include <QTcpSocket>

CAbandonBook::CAbandonBook(QObject * parent)
    :INetWorking(parent)
{
}

CAbandonBook::~CAbandonBook()
{
}

void CAbandonBook::prepareWriteData(QByteArray* block, void* param)
{
    // we cannot discrete this definition to the outside of the block,
    // and pass the reference to the funtion, or it will cause some
    // unknown problems, such as wrong streams. Maybe it is a bug of QT.
    QDataStream out(block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);

    protocal::ClientAbandonBook* pClientAbandonBook = (protocal::ClientAbandonBook*)param;

    // output the head information
    out << (qint32)(pClientAbandonBook->source) \
        << (qint32)(pClientAbandonBook->cli_command) \
        << (qint32)(pClientAbandonBook->size);

    // output the body information
    char * temp = new char [10];

    // we must do so, because the constructor of QString(char*) will truncate the
    // remaining space after '\0', and we need them to occupy the space.
    memcpy(temp, pClientAbandonBook->usr_id, 10);
    out << temp;

    delete [] temp;
}

bool CAbandonBook::prepareReadData(QTcpSocket *pTcpSocket, protocal::BasePtc *basePtc)
{
    // we do not need to read data from server
}

void CAbandonBook::formResult()
{
    // we do not need to read data from server
}
